#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# 生成html模板

# data --> list
def __make_list_table(data):
    table = ''
    for record in data:
        row_str = '</td><td>'.join(record)
        tr = ''.join((tr_style, row_str, '</td>'))
        
        table = ''.join((table, tr))

    return table


# data --> dict
def __make_dict_tables(data, TABLE_CSS, HTML_HEADER):
    html_tables = ''
    for key,value in data.items():
        if not value:
            continue
        title = '<h3>'+key+'</h3><br>'
        
        table = __make_list_table(value)
        html_table = ''.join(('<div>', title, TABLE_CSS, HTML_HEADER, table, '</table>', '</div>'))
        
        html_tables = ''.join((html_tables, html_table))
    
    return html_tables
        
        
#   python生成html文件模板
#
#    data : 具体数据LIST    <DICT> or <LIST>
#    tips ： 备注    <STRING>
#    title ： 标题    <STRING>
#    header ： 表头    <LIST>
#    
#    RETURN : 
#        html    <STRING>
#
#
def make_html(data,tips,headers):
    HTML_BODY_START = '<html><body>'
    TIPS = '<font color="red">'+ tips +'</font>'
    TABLE_CSS = '<table border="1" cellpadding="1" cellspacing="1" style="border-collapse: collapse;">'
    
    HEADER_TD = '</td><td>'.join(headers)
    HTML_HEADER = ''.join(('<tr><td>', HEADER_TD, '</td><tr>'))
    
    HTML_BODY_END = '</body></html>'
    
    if isinstance(data, list):
        table = __make_list_table(data)
        table_info = ''.join((TABLE_CSS, HTML_HEADER, table, '</table>'))
    elif isinstance(data, dict):
        table_info = __make_dict_tables(data, TABLE_CSS, HTML_HEADER)
    else:
        exit(1)
    
    html = ''.join((HTML_BODY_START, TIPS, table_info, HTML_BODY_END))
    
    return html